/**
 * @author yinyunqi
 * @datetime 2018年3月12日
 * @Content 
 */
package com.zcl.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zcl.dao.User;
import com.zcl.mapper.UserMapper;
import com.zcl.service.AccidentService;
import com.zcl.service.BoatService;
import com.zcl.service.HelpService;
import com.zcl.service.LossService;
import com.zcl.service.SeaService;
import com.zcl.service.UserService;
import com.zcl.util.GetCurrentUserDetails;

@Controller
@RequestMapping
public class IndexController {
	@Autowired
	AccidentService accidentService;
	@Autowired
	SeaService seaService;
	@Autowired
	BoatService boatService;
	@Autowired
	LossService lossService;
	@Autowired 
	HelpService helpService;
	@Autowired
	UserService userService;
	@Autowired
	UserMapper userMapper;
	Logger logger = Logger.getLogger(IndexController.class);
	/*
	 * 首页
	 */
	@RequestMapping("/index")
	public String index(Model model) {
		UserDetails userDetails = GetCurrentUserDetails.currentUser();
		model.addAttribute("userName", userDetails.getUsername());
		String userName = userDetails.getUsername();
		User user = userService.userQueryByName(userName);
		model.addAttribute("user", user);
		model.addAttribute("userName", userName);
		return "index";
	}
	/*
	 * 后台首页
	 */
	@RequestMapping("/main")
	public String indexMain(Model model) {
		int accidentAmount = accidentService.accidentQuery().size();
		int seaAmount = seaService.seaQuery().size();
		int boatAmount = boatService.boatQuery().size();
		int lossAmount = lossService.lossQuery().size();
		int helpAmount = helpService.helpQuery().size();
		UserDetails userDetails = GetCurrentUserDetails.currentUser();
		model.addAttribute("userName", userDetails.getUsername());
		model.addAttribute("accidentAmount", accidentAmount);
		model.addAttribute("seaAmount", seaAmount);
		model.addAttribute("boatAmount", boatAmount);
		model.addAttribute("lossAmount", lossAmount);
		model.addAttribute("helpAmount", helpAmount);
		String lastlogintime = userMapper.userQueryByName(userDetails.getUsername()).getLastlogintime();
		model.addAttribute("lastlogintime",lastlogintime);
		return "main";
	}
	
	@ResponseBody
	@RequestMapping("/index/currentUser")
	public UserDetails indexCurrentUser() {
		UserDetails user= GetCurrentUserDetails.currentUser();
//		logger.info("获取当前登录名为："+userName+"并传递给main.js及cache.js");
		return user;
	}
//	/*
//	 * 事故信息
//	 */
//	@RequestMapping("/accident")
//	public String indexAccident() {
//		return "accident";
//	}
//	/*
//	 * 海域信息
//	 */
//	@RequestMapping("/sea")
//	public String indexSea() {
//		return "sea";
//	}
//	/*
//	 * 船舶信息
//	 */
//	@RequestMapping("/boat")
//	public String indexBoat() {
//		return "boat";
//	}
//	/*
//	 * 损失信息
//	 */
//	@RequestMapping("/loss")
//	public String indexLoss() {
//		return "loss";
//	}
//	/*
//	 * 救助信息
//	 */
//	@RequestMapping("/help")
//	public String indexHelp() {
//		return "help";
//	}
//	/*
//	 * 救助信息
//	 */
//	@RequestMapping("/role")
//	public String indexRole() {
//		return "role";
//	}
//	/*
//	 * 救助信息
//	 */
//	@RequestMapping("/echarts")
//	public String indexEcharts() {
//		return "echarts";
//	}
}
